//
//  PowerNum2Topic.swift
//  XingCeHelper
//
//  Created by 申铭 on 2022/8/5.
//
//  求底数

import UIKit

class PowerNum2Topic: BaseTopic {
    
    /// 值
    var value: Float = 0.0
    /// 底数
    var base: Float = 0
    /// 指数
    var exponential: Float = 1
    
    /// 输入的分子
    var inputBase: Float = 0
    /// 输入的分母
    var inputExponential: Float = 1
    
    init(base: Float, exponential: Float) {
        self.value = pow(base, exponential)
        self.base = base
        self.exponential = exponential
    }

    override func setInputAnswer(answer: String?, numerator: Float = 0.0, denominator: Float = 1) {
        self.inputBase = Float(denominator)
        self.inputExponential = Float(numerator)
    }
    
    override func getTopicDesc() -> String {
        return "\(getValueStr(value)) = ?^\(Int(exponential))"
    }
    
    override func answerIsCorrect() -> Bool {
        return base == inputBase
    }
    
    override func getResultDesc() -> NSMutableAttributedString {
        var sym = "√"
        var color = UIColor.black
        if !answerIsCorrect() {
            sym = "×"
            color = .red
        }
        
        let desc = "\(getValueStr(value)) = \(getValueStr(base))^\(Int(exponential))(\(getValueStr(inputBase))^\(Int(exponential))) \(sym)"
        
        let attri = NSMutableAttributedString(string: desc)
        let tmpRange = NSRange(location: desc.count - 1, length: 1)
        
        attri.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: tmpRange)
        return attri
    }
    
    private func getValueStr(_ value : Float) -> String {
        return value.decimalString(2)
    }
}




